<template>
	<view :style="$s.view_color">
		<my-navbar :name="nav_title"></my-navbar>
		<view v-if="!sensitive">
			<view class="tips-text" :class="tips_class" v-if="error_tips!=''">
				{{error_tips}}
			</view>
			<view class="cardno-upload">
				<image class="cardno-upload-base64" style="width: 100%;" mode="widthFix" :src="id_front_avatar"></image>
			</view>
			<view class="camera-but">
				<!-- #ifdef MP-WEIXIN -->
				<button class="my-but-primary" @click="wxFace()">开始人脸识别</button>
				<!-- #endif -->
				<!-- #ifdef MP-ALIPAY -->
				<button class="my-but-primary" @click="alipayFace()">开始人脸识别</button>
				<!-- #endif -->
			</view>
		</view>
		<view v-if="sensitive">
			<u-empty mode="data" textSize="16" text="请下单后 , 前往线下门店进行人脸核实 , 方可进行下一步操作" icon="/static/images/empty/list.png"></u-empty>
		</view>
	</view>
</template>

<script>
	import { mapState } from 'vuex'
	import $url from '@/common/url'
	import checkLogin from '@/mixins/loginCheck.js'
	// #ifdef MP-WEIXIN
	let plugin = requirePlugin('shargoodata')
	// #endif
	export default {
		mixins:[checkLogin],
		data() {
			return {
				nav_title:'信息核实',
				sensitive:true,
				id_front_avatar:"",
				order_id:0,
				error_tips:"",
				tips_class:"",
			}
		},
		onLoad(option){
			// this.$u.api.getConfig().then(res=>{
			// 	this.id_front_avatar=res.data.design.id_front_avatar
			// 	this.sensitive=res.data.base.is_app_audit
			// })
			if(option.id){
				this.order_id=option.id
				
				this.$u.api.getConfig().then(res=>{
					this.id_front_avatar=res.data.design.id_front_avatar
					this.sensitive=res.data.base.is_app_audit===1?true:false
				})
			}else{
				this.$c.error('缺少参数','index/index')
			}
		},
		onShow(){
			this.initData()
		},
		computed:{
			...mapState({
				gain_info:state => state.$gainInfo,
				back_info:state => state.$backInfo,
				start_date:state => state.$startDate,
				end_date:state => state.$endDate,
				carid:state => state.$carID,
			}),
		},
		components: {
		},
		methods: {
			initData(){
				
			},
			//微信检测人脸
			//#ifdef MP-WEIXIN
			wxFace(){
				let than=this
				let post={order_id:this.order_id}
				this.$u.api.faceConfing(post).then(d=>{
					let data=d.data
					plugin.ocrStart({
						token: data.token,
						platformNo: data.platformNo,
						orderNo: data.orderNo,
						url: '',
						extraInfo: {
							idName: data.id_name,
							idNumber:  data.id_code,
							isShowCamera: !than.sensitive
						},
						success: res => {
							res=JSON.parse(res)
							if(res.success===true && res.data.code==="200"){
								uni.$u.api.wxFaceVerify(post).then(ok_res=>{
									than.verify_ok(ok_res)
								})
							}else{
								than.verify_error(0,res.data.msg)
							}
							console.log(res) // 插件的最终返回结果
						}
					})
					
					uni.navigateTo({
						url: 'plugin-private://wxfd41bbd0f83ffe83/pages/face/index'
					})
				})
			},
			//#endif
			
			//支付宝检测人脸
			//#ifdef MP-ALIPAY
			alipayFace(){
				let than=this
				let post={order_id:this.order_id}
				uni.$u.api.alipayFace(post).then(conf=>{
					my.startAPVerify({
						url: conf.data.face_url,
						certifyId: conf.data.certify_id,
						success: function (res) {
							uni.$u.api.aliFaceVerify(post).then(ok_res=>{
								than.verify_ok(ok_res)
							})
						},
						fail: function (res) {
							than.verify_error(res.resultStatus,res.result.errorCode)
						},
						complete: function (res) {
							// than.verify_error(res.resultStatus,res.result.errorCode)
							console.log('complete', res)
						}
					})
				})
			},
			//#endif
			
			//认证成功处理
			verify_ok(res2){
				if(res2.status){
					this.tips_class="success"
					this.error_tips="检验成功！"
					setTimeout(res=>{
						this.$c.success('检验成功！','flow/'+res2.data.url+'/index?id='+this.order_id)
					},500)
				}
			},
			//认证失败处理
			verify_error(code,msg){
				if(code==="4000"){
					switch(msg){
						case "NAME_NOT_MATCH_CERT_NO":
							msg="身份信息与支付宝认证信息不匹配"
							break
						case "USER_IS_NOT_CERTIFY":
							msg="用户未认证"
							break;
						default:
							msg="认证不通过"
					}
				}else if(code==="6001"){
					msg="您取消了认证"
				}
				this.tips_class="error"
				this.error_tips=msg
				console.log(msg)
				setTimeout(res=>{
					than.$c.error(msg)
				},500)
			},
		}
	}
</script>

<style lang="scss">
	page{
		background-color:#FFFFFF;
	}
	.camera-title{
		font-size: 32rpx;
		margin-top: 50rpx;
		text-align: center;
	}
	.camera-box{
		display: flex;
		flex-direction:column;
		align-items: center;
		justify-content: center;
		margin: 80rpx auto;
	}
	.camera-but{
		display: flex;
		justify-content: space-around;;
		flex-direction:row ;
		margin: 0rpx auto;
		padding: 0rpx 60rpx;
	}
	.camera-but view{
		width: 80%;
	}
	
	.cardno-upload {
	    display: -ms-flexbox;
	    display: flex;
	    flex-direction: column;
	    align-items: center;
	    justify-content: center;
		padding: 15rpx;
		margin: 40rpx auto;
	    border-radius: 10rpx;
	}
	
	.page-idcard .cardno-upload-base64 {
	    object-fit: contain;
	}
	.tips-text{
		font-size: 28rpx;
		color: #FFFFFF;
		width: 100%;
		text-align: center;
		margin-top: 0rpx;
		padding: 20rpx;
		margin-bottom: 30rpx;
	}
	
	.error{
		background-color: $u-error;
	}
	.success{
		background-color: $u-success;
	}
</style>
